$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/healthheadline/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '医生', name: 'dname', index: 'dname', width: 80},
			{ label: '内容', name: 'content', index: 'content', width: 150 },
            { label: '图片地址', name: 'imgurl', index: 'imgurl', width: 80 , formatter: function(cellvalue, options, rowdata) {
                    return '<img src='+rowdata.imgurl+' width="50" height="50"/>';
            }},
			{ label: '添加时间', name: 'createtime', index: 'createtime', width: 80 },
            { label: '操作', name: 'sy', width: 70,align:'center',formatter: function(cellvalue, options, rowdata) {
                    return "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.update("+ rowdata.id +")'>" +
                        "修改</a>&nbsp;&nbsp;" +
                        "<a style='cursor:pointer;' class='btn btn-primary' onclick='vm.del("+ rowdata.id +")'>" +
                        "删除</a>";
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        fileList1: [],
        q:{
            dname:null
        },
		showList: true,
		title: null,
		healthheadline: {
		    username:null
        },
        users:{}
	},

    healthheadline: {
        username:null
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
            vm.queryDoctor();
			vm.showList = false;
			vm.title = "新增";

		},
		update: function (id) {
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id);
            vm.queryDoctor();
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.healthheadline.id == null ? "sys/healthheadline/save" : "sys/healthheadline/update";
                vm.healthheadline.imgurl = '';
                if(vm.fileList1.length>0){
                    vm.healthheadline.imgurl = vm.fileList1[0].url;
                }
                vm.healthheadline.answer = tinyMCE.activeEditor.getContent() ;
                if(vm.healthheadline.userid == ''|| typeof vm.healthheadline.userid == 'undefined'){
                    alert("医生不能为空");
                }else if(vm.fileList1.length ==0){
                    alert("图片不能为空");
                }else if(vm.healthheadline.content == ''|| typeof vm.healthheadline.content == 'undefined'){
                    alert("问题不能为空");
                }else if(vm.healthheadline.answer == ''|| typeof vm.healthheadline.answer == 'undefined'){
                    alert("解答不能为空");
                }else{
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.healthheadline),
                        success: function(r){
                            if(r.code === 0){
                                layer.msg("操作成功", {icon: 1});
                                vm.reload();
                                $('#btnSaveOrUpdate').button('reset');
                                $('#btnSaveOrUpdate').dequeue();
                            }else{
                                layer.alert(r.msg);
                                $('#btnSaveOrUpdate').button('reset');
                                $('#btnSaveOrUpdate').dequeue();
                            }
                        }
                    });
                }

			});
		},
		del: function (id) {
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/healthheadline/delete/"+id,
                        contentType: "application/json",
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
        queryDoctor: function(){
            $("#doctor_id").html("");
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/doctor/doctorlist",
                success: function (r) {
                    var temp = "";
                    $.each(r.list, function (i) {

                        temp +="<option value='"+r.list[i].userid+"'>"+r.list[i].name+"</option>";

                    });
                    $("#doctor_id").append(temp);
                }
            });
        },
        handleRemove: function(file, fileList) {
            console.log(fileList);
            if(fileList.length>0){
                vm.fileList1 = [];
                for(var i=0;i<fileList.length;i++){
                    vm.fileList1.push({name: fileList[i].name,url: fileList[i].url});

                }
            }
            console.log(vm.fileList1);

        },
        handlePreview: function(file) {
            console.log(file);
        },

        handsuccess: function(response, file, fileList) {
            vm.fileList1.push({name: response.url,url: response.url});

        },
		getInfo: function(id){
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/healthheadline/info/"+id,
                success: function(r){
                    vm.healthheadline = r.healthheadline;
                    tinyMCE.activeEditor.setContent(r.healthheadline.answer)
                    vm.fileList1 = [];
                    if(r.healthheadline.imgurl!=null&&r.healthheadline.imgurl!=''){
                        vm.fileList1.push({name: r.healthheadline.imgurl,url: r.healthheadline.imgurl});
                    }
                }
            });
		},
        getUsers: function () {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/doctor/doctorlist",
                success: function (r) {
                    console.log(r);
                    if (r.code == 0) {
                        vm.users = r.list;
                    } else {
                        layer.alert(r.msg);
                    }
                }
            });
            console.log(vm);
        },
        setUsername: function (id) {
            for (var i = 0; i < vm.users.length; i++) {
                if (id == vm.users[i].id) {
                    vm.healthheadline.username = vm.users[i].username;
                }
            }
            console.log(vm);
        },
		reload: function (event) {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            if((startime!=''&&typeof startime!='undefined')&&(endtime!=''&&typeof endtime!='undefined')){
                var st = new Date(startime).getTime();
                var et = new Date(endtime).getTime();
                if(st>et){
                    alert("开始时间不能大于结束时间");
                    return;
                }
            }
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'dname':vm.q.dname,'startime':startime,'endtime':endtime},
                page:page
            }).trigger("reloadGrid");
		}
	}
});